<template>
  <div>
    <!-- 编号、频率 -->
    <a-form-model :model="form" :rules="rulesform" layout="inline" ref="form" style="margin-top: 10px">
      <a-row style="display: flex;align-items: center;">
        <a-col :span="6">
          <a-form-model-item prop="N">
            <template #label>
              <span class="custom-label">节点编号</span>
            </template>
            <a-select v-model="form.N" placeholder="请选择节点编号" style="width: 230px">
              <a-select-option :value="index.toString()" v-for="(item,index) in 20" :key="index">
                {{ index.toString() }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="6">
          <a-form-model-item prop="F">
            <template #label>
              <span class="custom-label">采集频率</span>
            </template>
            <a-input placeholder="请输入采集频率" v-model="form.F" style="width: 230px"/>
          </a-form-model-item>
        </a-col>
        <a-col :span="6">
          <a-form-model-item>
            <template #label>
              <span class="custom-label" style="margin-left: 10px;">备注</span>
            </template>
            <a-input placeholder="请输入备注" v-model="form.Note" style="width: 230px"/>
          </a-form-model-item>
        </a-col>
        <a-col :span="6">
          <div>
            <img :src="QRCodeImg" alt="" srcset="">
            <div style="color: #000">{{ SensorCode }}</div>
          </div>
        </a-col>
      </a-row>
    </a-form-model>
    <a-divider orientation="left" style="letter-spacing: 5px;font-weight: bold">基础配置</a-divider>
    <!-- 基础配置 -->
    <div class="table-add-swl">
      <a-button type="primary" icon="read" @click="configHttp()" style="margin-right: 16px">载入</a-button>
      <a-button type="primary" icon="plus" @click="addConfig()" style="margin-right: 16px">新增</a-button>
      <a-button type="primary" icon="save" @click="saveConfig()">保存</a-button>
    </div>
    <!--  展示表单  -->
    <a-table bordered :columns="Columns"
             :data-source="form.Tabs"
             :pagination="false"
             :rowKey="(record,index)=>{return index}"
             size="small">
      <div slot="Mode" slot-scope="Mode">
        {{ Mode !== "" && Mode !== null && Mode !== undefined ? ModeList[Number(Mode)>15?Number(Mode)-9:Number(Mode)].name : '' }}
      </div>
      <!-- 操作 -->
      <div slot="operation" style="margin-left: -16px;" slot-scope="record, index, indent">
        <a-row type="flex" justify="space-around">
          <a-col :span="4">
            <a-button type="link" @click="addConfig(record, indent)">编辑</a-button>
          </a-col>
          <a-col :span="4">
            <a-button type="link" @click="moveUp(record, indent)">上移</a-button>
          </a-col>
          <a-col :span="4">
            <a-button type="link" @click="moveDown(record, indent)">下移</a-button>
          </a-col>
          <a-col :span="4">
            <a-button type="link" @click="copyRecord(record, indent)">克隆</a-button>
          </a-col>
          <a-col :span="4">
            <a-popconfirm
                title="是否删除此行?"
                ok-text="是"
                cancel-text="否"
                @confirm="deleteRecord(indent)"
            >
              <a-button type="link" style="color:#F08080">删除</a-button>
            </a-popconfirm>
          </a-col>
        </a-row>
      </div>
    </a-table>
    <!-- 新增、编辑  -->
    <a-modal v-model="showLine" v-if="showLine" :title="`${isLineAdd?'新增':'编辑'}配置`" @ok="lineOk"
             width="1000px">
      <a-form-model :model="dataLine" :rules="rulesLine" ref="formLine">
        <!-- 寄存器个数:0-2\小数位数:0-3\采集延时:20-5000       -->
        <a-divider>基础配置</a-divider>
        <div style="display: flex;justify-content: space-around">
          <a-form-model-item label="参数名" style="width: 23%;">
            <a-input v-model="dataLine.PName" placeholder="请输入参数名"/>
          </a-form-model-item>
          <a-form-model-item label="发射信道" style="width: 23%;">
            <a-input v-model="dataLine.TxChannel" placeholder="请输入发射信道"/>
          </a-form-model-item>
          <a-form-model-item label="接收信道" style="width: 23%;">
            <a-input v-model="dataLine.RxChannel" placeholder="请输入接收信道"/>
          </a-form-model-item>
          <a-form-model-item label="modbus地址" style="width: 23%;">
            <a-input v-model="dataLine.ModbusAddr" placeholder="请输入modbus地址"/>
          </a-form-model-item>
        </div>
        <div style="display: flex;justify-content: space-around">
          <a-form-model-item label="寄存器地址" style="width: 23%;">
            <a-input v-model="dataLine.RegAddr" placeholder="请输入寄存器地址"/>
          </a-form-model-item>
          <a-form-model-item label="寄存器个数" style="width: 23%;" prop="RegCount">
            <a-input v-model="dataLine.RegCount" placeholder="请输入寄存器个数"/>
          </a-form-model-item>
          <a-form-model-item label="放大比例" style="width: 23%;">
            <a-input v-model="dataLine.Enlange" placeholder="请输入放大比例"/>
          </a-form-model-item>
          <a-form-model-item label="偏移量" style="width: 23%;">
            <a-input v-model="dataLine.Offset" placeholder="请输入偏移量"/>
          </a-form-model-item>
        </div>
        <div style="display: flex;justify-content: space-around">
          <a-form-model-item label="存储模式" style="width: 23%;">
            <a-select v-model="dataLine.Mode" placeholder="请输入存储模式">
              <a-select-option :value="item.value" v-for="(item,index) in ModeList" :key="index">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="小数位数" style="width: 23%;" prop="Count">
            <a-input v-model="dataLine.Count" placeholder="请输入小数位数"/>
          </a-form-model-item>
          <a-form-model-item label="采集延时" style="width: 23%;" prop="Delay">
            <a-input v-model="dataLine.Delay" placeholder="请输入采集延时"/>
          </a-form-model-item>
          <div style="width: 23%;"></div>
        </div>
        <a-divider>转发配置</a-divider>
        <div style="display: flex;justify-content: space-around">
          <a-form-model-item label="发射信道" style="width: 23%;">
            <a-input v-model="dataLine.ForwardTxChannel" placeholder="请输入发射信道"/>
          </a-form-model-item>
          <a-form-model-item label="接收信道" style="width: 23%;">
            <a-input v-model="dataLine.ForwardRxChannel" placeholder="请输入接收信道"/>
          </a-form-model-item>
          <a-form-model-item label="转发modbus" style="width: 23%;">
            <a-input v-model="dataLine.ForwardModbus" placeholder="请输入转发modbus"/>
          </a-form-model-item>
          <a-form-model-item label="寄存器地址" style="width: 23%;">
            <a-input v-model="dataLine.ForwardReg" placeholder="请输入寄存器地址"/>
          </a-form-model-item>
        </div>
      </a-form-model>
    </a-modal>
    <!-- 生成框  -->
    <a-textarea placeholder="生成字符串" :rows="4" style="margin-top: 20px" v-model="strConfig" disabled/>
    <div class="table-add-swl">
      <a-button type="primary" icon="read" @click="configHttp()" style="margin-right: 16px">载入</a-button>
      <a-button type="primary" icon="save" @click="saveConfig()" style="margin-right: 16px">保存</a-button>
      <a-button type="primary" icon="cluster" @click="createString()" style="margin-right: 16px">生成</a-button>
      <a-button type="primary" icon="sound" @click="sendConfig()">发送</a-button>
    </div>
    <a-divider orientation="left" style="letter-spacing: 5px;font-weight: bold">RTU转发配置</a-divider>
    <!-- 中间5位 00651的话 支持16个,其他为1个   -->
    <div class="arr-list">
      <div v-for="(item,index) in GwNum" :key="index" class="arr-list-child">
        <span>目标设备{{ index }}：</span>
        <a-input v-model="item.TDevName" style="width: 180px" @blur="getSelectList(item)"/>
        <span>节点 sn：</span>
        <a-select v-model="item.IParam" style="width: 180px">
          <a-select-option :value="i.toString()" v-for="(child,i) in item.selectList" :key="child.SensorCode">
            {{ child.SensorCode }}
          </a-select-option>
        </a-select>
        <a-button type="primary" style="margin:0 10px" @click="synchronization(item)">同步配置</a-button>
        <a-button type="primary" @click="sendConfigFileHttp(item)">发送</a-button>
      </div>
    </div>
    <a-textarea placeholder="生成字符串" :rows="4" style="margin-top: 20px" v-model="strRTUConfig" disabled/>
    <div class="table-add-swl">
      <a-button type="primary" icon="read" @click="configHttp()" style="margin-right: 16px">载入</a-button>
      <a-button type="primary" icon="save" @click="saveRTUConfig()" style="margin-right: 16px">保存</a-button>
      <a-button type="primary" icon="cluster" @click="createRTUString()" style="margin-right: 16px">生成</a-button>
      <a-button type="primary" icon="sound" @click="sendRTUConfig()">发送</a-button>
    </div>
  </div>
</template>

<script>
import QRCode from 'qrcode'
import {
  configDecodeId,
  configSaveFile,
  sendRtuConfig,
  saveRtuFile,
  getSensorByGw,
  saveRtuCopy,genRtuConfig,
  sendConfig,
  sendConfigFile, getConfigStatus
} from "@/services/sensor";
import {getMessageStatus} from "@/services/led";

export default {
  name: "Sensor",
  props: ['SensorCode', 'ProductKey', 'GwCode'], //节点设备SN
  watch: {
    'SensorCode'() {
      // 监听变化
      if (this.SensorCode) {
        this.addQRCode()
      } else {
        this.QRCodeImg = ''
      }
    },
    'GwCode'() {
      this.refreshGwCode()
    },
    deep: true
  },
  data() {
    return {
      QRCodeImg: '',
      form: {
        N: undefined,//节点编号
        F: undefined,//采集频率
        Gw: undefined,
        Pro: undefined,
        Sn: undefined,
        Note: '',
        Tabs: [],
        Arr: []
      },
      rulesform: {
        N: {required: true, message: '请选择节点编号', trigger: 'select'},//节点编号
        F: {required: true, message: '请输入采集频率', trigger: 'blur'},//采集频率
      },
      ModeList: [{
        name: 'AB', value: '0'
      }, {
        name: 'BA', value: '1'
      }, {
        name: 'ABCD', value: '2'
      }, {
        name: 'DCBA', value: '3'
      }, {
        name: 'CDAB', value: '4'
      }, {
        name: '00AB', value: '5'
      }, {
        name: 'FLOAT', value: '6'
      },{
        name: 'AB(无符号)', value: '16'
      }, {
        name: 'BA(无符号)', value: '17'
      }, {
        name: 'ABCD(无符号)', value: '18'
      }, {
        name: 'DCBA(无符号)', value: '19'
      }, {
        name: 'CDAB(无符号)', value: '20'
      }, {
        name: '00AB(无符号)', value: '21'
      }],
      Columns: [
        {
          title: "序号",
          dataIndex: "key",
          key: "key",
          align: "center",
          width: 45,
          customRender: (text, record, index) => `${index}`,//此处为重点
        },
        {
          title: '基础配置',
          align: "center",
          children: [{
            title: '参数名',
            align: "center",
            dataIndex: 'PName',
            width: 90,
            key: 'PName'
          }, {
            title: '发射信道',
            align: "center",
            dataIndex: 'TxChannel',
            width: 80,
            key: 'TxChannel'
          }, {
            title: '接收信道',
            align: "center",
            dataIndex: 'RxChannel',
            width: 80,
            key: 'RxChannel'
          }, {
            title: 'modbus地址',
            align: "center",
            dataIndex: 'ModbusAddr',
            width: 70,
            key: 'ModbusAddr'
          }, {
            title: '寄存器地址',
            align: "center",
            dataIndex: 'RegAddr',
            width: 85,
            key: 'RegAddr'
          }, {
            title: '寄存器个数',
            align: "center",
            dataIndex: 'RegCount',
            width: 85,
            key: 'RegCount'
          }, {
            title: '放大比例',
            align: "center",
            dataIndex: 'Enlange',
            width: 80,
            key: 'Enlange'
          }, {
            title: '偏移量',
            align: "center",
            dataIndex: 'Offset',
            width: 80,
            key: 'Offset'
          }, {
            title: '存储模式',
            align: "center",
            dataIndex: 'Mode',
            width: 80,
            key: 'Mode',
            scopedSlots: {customRender: 'Mode'}
          }, {
            title: '小数位数',
            align: "center",
            dataIndex: 'Count',
            width: 50,
            key: 'Count'
          }, {
            title: '采集延时',
            align: "center",
            dataIndex: 'Delay',
            width: 80,
            key: 'Delay'
          }]
        },
        {
          title: '转发配置',
          align: "center",
          children: [{
            title: '发射信道',
            align: "center",
            dataIndex: 'ForwardTxChannel',
            width: 80,
            key: 'ForwardTxChannel'
          }, {
            title: '接收信道',
            align: "center",
            dataIndex: 'ForwardRxChannel',
            width: 80,
            key: 'ForwardRxChannel'
          }, {
            title: '转发modbus',
            align: "center",
            dataIndex: 'ForwardModbus',
            width: 80,
            key: 'ForwardModbus'
          }, {
            title: '寄存器地址',
            align: "center",
            dataIndex: 'ForwardReg',
            width: 85,
            key: 'ForwardReg'
          },]
        },
        {
          title: '操作',
          key: 'operation',
          align: 'center',
          width: 210,
          scopedSlots: {customRender: 'operation'},
        }],
      showLine: false,//是否显示弹窗
      isLineAdd: false,//是否是新增
      dataLineNum: undefined,//编辑：下标
      dataLine: {
        Count: undefined,
        Delay: undefined,
        Enlange: undefined,
        ForwardModbus: undefined,
        ForwardReg: undefined,
        ForwardRxChannel: undefined,
        ForwardTxChannel: undefined,
        ModbusAddr: undefined,
        Mode: undefined,
        Offset: undefined,
        PName: undefined,
        RegAddr: undefined,
        RegCount: undefined,
        RxChannel: undefined,
        TxChannel: undefined,
      },
      rulesLine: {
        RegCount: [{validator: this.validateIntegerJCQ, trigger: 'blur'}],
        Count: [{validator: this.validateIntegerXSW, trigger: 'blur'}],
        Delay: [{validator: this.validateIntegerYS, trigger: 'blur'}]
      },
      //生成的字符串
      strConfig: '',
      strRTUConfig: '',
      // 判断中间5位 00651的话 支持16个,其他为1个
      GwNum: []
    }
  },
  methods: {
    // 二维码
    addQRCode() {
      QRCode.toDataURL(this.SensorCode)
          .then(url => {
            this.QRCodeImg = url
          })
    },
    /*
    *
    * 【基础配置部分】
    *
    */
    // 读取
    async configHttp() {
      let _this = this;
      if (_this.SensorCode) {
        // 二维码
        _this.addQRCode()
        // 清空预览已选
        let res = await configDecodeId(_this.SensorCode)
        if (res.Code === 0) {
          // 重组基础配置
          if (res.Data.Tabs) {
            let tabsList = []
            res.Data.Tabs.forEach((item) => {
              let itemNum = 0
              // if (item.Count || item.Delay || item.Enlange || item.ForwardModbus || item.ForwardReg
              //     || item.ForwardRxChannel || item.ForwardTxChannel || item.ModbusAddr || item.Mode
              //     || item.Offset || item.PName || item.RegAddr || item.RegCount || item.RxChannel || item.TxChannel) {
              if(item.PName){
                itemNum++;
              }
              if (itemNum > 0) {
                tabsList.push(item)
              }
            })
            res.Data.Tabs = tabsList
          }
          // 重组RTU转发配置
          _this.refreshGwCode()
          if (res.Data.Arr) {
            _this.GwNum.length = res.Data.Arr.length
            for (let i = 0; i < res.Data.Arr.length; i++) {
              if (_this.GwNum.length >= i) {
                _this.GwNum[i].TDevName = res.Data.Arr[i].TDevName
                _this.GwNum[i].IParam = res.Data.Arr[i].IParam
                _this.GwNum[i].TProduct = res.Data.Arr[i].TProduct
                _this.getSelectList(_this.GwNum[i])
              }
            }
          }
          _this.form = res.Data
        } else {
          _this.$message.error('读取配置失败');
        }
      } else {
        _this.$message.error('请选择设备');
      }
    },
    // 新增||编辑
    addConfig(record, indent) {
      let _this = this
      if (record) {
        _this.isLineAdd = false
        _this.dataLine = JSON.parse(JSON.stringify(record))
        _this.dataLineNum = indent
        _this.showLine = true
      } else {
        _this.dataLine = {
          Count: undefined,
          Delay: undefined,
          Enlange: undefined,
          ForwardModbus: undefined,
          ForwardReg: undefined,
          ForwardRxChannel: undefined,
          ForwardTxChannel: undefined,
          ModbusAddr: undefined,
          Mode: undefined,
          Offset: undefined,
          PName: undefined,
          RegAddr: undefined,
          RegCount: undefined,
          RxChannel: undefined,
          TxChannel: undefined
        }
        _this.isLineAdd = true
        _this.showLine = true
      }
    },
    // 格式判断[寄存器]
    validateIntegerJCQ(rule, value, callback) {
      if (value && !Number.isInteger(Number(value))) {
        callback(new Error('请输入整数'));
      } else {
        if (value < 0 || value > 2) {
          callback(new Error('寄存器个数：0~2'));
        } else {
          callback();
        }
      }
    },
    // 格式判断[小数]
    validateIntegerXSW(rule, value, callback) {
      if (value && !Number.isInteger(Number(value))) {
        callback(new Error('请输入整数'));
      } else {
        if (value < 0 || value > 3) {
          callback(new Error('小数位数：0~3'));
        } else {
          callback();
        }
      }
    },
    // 格式判断[采集延时]
    validateIntegerYS(rule, value, callback) {
      if (value && !Number.isInteger(Number(value))) {
        callback(new Error('请输入整数'));
      } else {
        if (value < 20 || value > 5000) {
          callback(new Error('采集延时：20~5000'));
        } else {
          callback();
        }
      }
    },
    // 新增||编辑 确定
    lineOk() {
      let _this = this
      _this.$refs.formLine.validate(async valid => {
        if (valid) {
          if (_this.isLineAdd) {
            _this.form.Tabs.push(_this.dataLine)
          } else {
            _this.form.Tabs[_this.dataLineNum] = _this.dataLine
          }
          _this.showLine = false
        }
      });
    },
    // 上移
    moveUp(record, indent) {
      let _this = this
      if (indent > 0) {
        let upData = []
        upData = _this.form.Tabs[indent - 1];
        _this.form.Tabs.splice(indent - 1, 1);//删除上一条的数据
        _this.form.Tabs.splice(indent, 0, upData);//将上一条数据插入到当前位置
      } else {
        _this.$message.info(`已经是第一条，上移失败`);
      }
    },
    // 下移
    moveDown(record, indent) {
      let _this = this
      if (indent + 1 === _this.form.Tabs.length) {
        _this.$message.info('已经是最后一条，下移失败');
      } else {
        let downData = _this.form.Tabs[indent + 1];
        _this.form.Tabs.splice(indent + 1, 1);
        _this.form.Tabs.splice(indent, 0, downData);
      }
    },
    // 克隆
    copyRecord(record, indent) {
      let _this = this
      _this.form.Tabs.splice(indent + 1, 0, record)
    },
    // 删除
    deleteRecord(indent) {
      let _this = this
      _this.form.Tabs.splice(indent, 1)
    },
    // 配置保存
    saveConfig() {
      let _this = this;
      // 判断是否选择了设备
      if (_this.SensorCode) {
        // 表单验证是否输入基础配置
        _this.$refs.form.validate(async valid => {
          if (valid) {
            // 生成
            _this.createString()
            let dataHttp = {
              Gw: _this.GwCode,
              Pro: _this.ProductKey,
              Sn: _this.SensorCode,
              N: _this.form.N,//节点编号
              F: _this.form.F,//采集频率
              Note: _this.form.Note,
              Tabs: _this.form.Tabs
            }
            let res = await configSaveFile(dataHttp)
            if (res.Message === 'Success') {
              this.$message.success('保存成功');
            } else {
              _this.$message.error('保存失败');
            }
          }
        });
      } else {
        _this.$message.error('请选择设备');
      }
    },
    // 发送
    sendConfig() {
      let _this = this;
      // 判断是否选择了设备
      if (_this.SensorCode) {
        // 表单验证是否输入基础配置
        _this.$refs.form.validate(async valid => {
          if (valid) {
            // 生成
            _this.createString()
            let dataHttp = {
              Gw: _this.GwCode,
              Pro: _this.ProductKey,
              Str: _this.strConfig
            }
            let res = await sendConfig(dataHttp)

            if (res.Message === 'Success') {
              this.$message.info('发送成功');
              // 等待设备回应
              setTimeout(function () {
                _this.sendRTUStatus(_this.GwCode)
              }, 3000)
            } else {
              _this.$message.error('发送失败');
            }
          }
        });
      } else {
        _this.$message.error('请选择设备');
      }
    },
    /*
    *
    * 【RTU配置部分】
    *
    */
    // 刷新RTU数量
    refreshGwCode() {
      let _this = this
      // 监听变化
      if (_this.GwCode) {
        // 首次进入的时候赋空值
        if (_this.GwNum.length === 0) {
          if (_this.GwCode.substring(5, 10) === '00651') {
            for (let i = 0; i < 16; i++) {
              _this.GwNum.push({
                'TDevName': '',
                'IParam': '',
                'TProduct': '',
                'selectList': []
              })
            }
          } else {
            _this.GwNum = [{
              'TDevName': '',
              'IParam': '',
              'TProduct': '',
              'selectList': []
            }]
          }
        } else {
          let newList = []
          // 如果之前是16个
          if (_this.GwNum.length === 16) {
            for (let i = 0; i < _this.GwNum.length; i++) {
              // 现在也是16
              if (_this.GwCode.substring(5, 10) === '00651' && i < 16) {
                newList.push(_this.GwNum[i])
              } else {
                // 现在也是1
                newList = [_this.GwNum[0]]
              }
            }
          } else {
            // 如果之前是1个
            if (_this.GwCode.substring(5, 10) === '00651') {
              // 现在是16
              for (let i = 0; i < 16; i++) {
                if (i === 0) {
                  newList = _this.GwNum
                } else {
                  newList.push({
                    'TDevName': '',
                    'IParam': '',
                    'TProduct': '',
                    'selectList': []
                  })
                }
              }
            } else {
              // 现在也是1
              newList = _this.GwNum
            }
          }
          _this.GwNum = newList
        }
      } else {
        _this.GwNum = []
      }
    },
    // 获取RTU对应选择 && RTU 输入框失去焦点的时候 获取筛选
    async getSelectList(item) {
      let res = await getSensorByGw(item.TDevName)
      if (res.Code === 0) {
        item.selectList = res.Data
      }
    },
    // RTU同步
    async synchronization(item) {
      let _this = this
      if (item.TDevName === '') {
        _this.$message.info('目标设备为空');
      } else {
        let res = await saveRtuCopy({
          'nodeId': item.IParam === "" ? '0' : item.IParam,
          'source': _this.SensorCode,
          'tDevName': item.TDevName,
        })
        if (res.Message === 'Success') {
          _this.$message.success('同步成功');
        } else {
          _this.$message.info('同步失败');
        }
      }
    },
    // RTU保存
    saveRTUConfig() {
      let _this = this;
      // 判断是否选择了设备
      if (_this.SensorCode) {
        // 表单验证是否输入基础配置
        _this.$refs.form.validate(async valid => {
          if (valid) {
            let newGw = []
            _this.GwNum.forEach((item) => {
              newGw.push({
                'IParam': item.IParam === "" ? 0 : Number(item.IParam),
                'TDevName': item.TDevName,
                'TProduct': ''
              })
            })
            let dataHttp = {
              Gw: _this.GwCode,
              Pro: _this.ProductKey,
              Sn: _this.SensorCode,
              N: _this.form.N,//节点编号
              Arr: newGw
            }
            let res = await saveRtuFile(dataHttp)
            if (res.Message === 'Success') {
              this.$message.success('保存成功');
            } else {
              _this.$message.error('保存失败');
            }
          }
        });
      } else {
        _this.$message.error('请选择设备');
      }
    },
    // RTU发送[单个]
    async sendConfigFileHttp(item) {
      let _this = this
      if (item.TDevName === '') {
        _this.$message.info('目标设备为空');
      } else {
        let res = await sendConfigFile({
          'nodeId': item.IParam === "" ? '0' : item.IParam,
          'tDevName': item.TDevName,
        })
        if (res.Message === 'Success') {
          _this.$message.success('发送成功');
          setTimeout(function () {
            _this.sendRTUStatus(item.TDevName)
          }, 3000)
        } else {
          _this.$message.info('发送失败');
        }
      }
    },
    // RTU发送，等待设备回应
    async sendRTUStatus(TDevName) {
      let _this = this;
      let res = await getConfigStatus(TDevName)
      if (res.Code === -1) {
        _this.$message.error('设备回应配置失败');
      } else {
        _this.$message.success('设备回应成功');
      }
    },
    // RTU发送
    sendRTUConfig() {
      let _this = this;
      // 判断是否选择了设备
      if (_this.SensorCode) {
        // 表单验证是否输入基础配置
        _this.$refs.form.validate(async valid => {
          if (valid) {
            let dataHttp = {
              Gw: _this.GwCode,
              Pro: _this.ProductKey,
              N: _this.form.N,//节点编号
              Arr: _this.form.Arr
            }
            let res = await sendRtuConfig(dataHttp)
            if (res.Message === 'Success') {
              this.$message.success('发送成功');
              // 等待设备回应
              setTimeout(function () {
                _this.sendRTUrespond(1)
              }, 3000)
            } else {
              _this.$message.error('发送失败');
            }
          }
        });
      } else {
        _this.$message.error('请选择设备');
      }
    },
    // RTU发送，等待设备回应
    async sendRTUrespond(num) {
      let _this = this;
      let res = await getMessageStatus(_this.form.Gw)
      if (res.Data === '') {
        _this.$message.info(`等待设备回应，请稍候`);
        if (num < 5) {
          setTimeout(function () {
            _this.sendRTUrespond(num + 1)
          }, 3000)
        }
      } else {
        _this.$message.success('设备回应成功');
      }
    },
    /*
    *
    * 【生成】
    *
    */
    // 生成
    createString() {
      //let config={}
      let _this = this;
      if (this.form.N===null||this.form.N===''){
        _this.$message.error('节点编号为空');
        return;
      }
      if (this.form.F===null||this.form.F===''){
        _this.$message.error('采集频率为空');
        return;
      }
      console.log()
      let SensorParamSet=this.form.N+','+this.form.F+','+this.SensorCode;
      let table=this.form.Tabs
      for (let i = 0; i < table.length; i++) {
        console.log(table[i])
        let num1=Number(table[i].TxChannel)*256*256+Number(table[i].RxChannel)*256+Number(table[i].ModbusAddr);
        let num2=Number(table[i].ForwardTxChannel)*256*256+Number(table[i].ForwardRxChannel)*256+Number(table[i].ForwardModbus);
        let str='['+table[i].PName+','+
            num1+','+
            table[i].RegAddr+','+
            table[i].RegCount+','+
            num2+','+
            table[i].ForwardReg+','+
            table[i].Enlange+','+
            table[i].Offset+','+
            table[i].Mode+','+
            table[i].Count+','+
            table[i].Delay+']'
        SensorParamSet=SensorParamSet+','+str;
      }
      let json={
        "SensorParamSet":SensorParamSet
      }
      this.strConfig=JSON.stringify(json);
    },
    // RTU 生成
    createRTUString() {
      let _this = this;
      // 判断是否选择了设备
      if (_this.SensorCode) {
        // 表单验证是否输入基础配置
        _this.$refs.form.validate(async valid => {
          if (valid) {
            let dataHttp = {
              Gw: _this.GwCode,
              Pro: _this.ProductKey,
              N: _this.form.N,//节点编号
              Arr: _this.form.Arr
            }
            let res = await genRtuConfig(dataHttp)
            if (res.Code === 0 && res.Message === "Success") {
              _this.strRTUConfig = res.Data
            }else {
              _this.$message.error('生成失败');
            }
          }
        });
      } else {
        _this.$message.error('请选择设备');
      }

    },
  }
}
</script>

<style scoped>
.custom-label {
  width: 60px;
  display: inline-block;
  text-align: center;
}

.arr-list {
  display: flex;
  flex-wrap: wrap;
}

.arr-list-child {
  width: 50%;
  display: flex;
  align-items: center;
  margin: 8px 0;
}

.arr-list-child span {
  width: 90px;
  text-align: right;
}

/deep/ .ant-form-inline .ant-form-item-with-help {
  margin-bottom: 0;
}

.table-add-swl {
  margin: 15px 0;
  display: flex;
  justify-content: flex-end;
}

/deep/ .ant-input[disabled] {
  background-color: #fefefe;
  color: #000;
  pointer-events: auto;
}

/deep/ .ant-table-small > .ant-table-content > .ant-table-body > table > .ant-table-tbody > tr > td {
  padding: 5px;
}

/deep/ .ant-table-small > .ant-table-content > .ant-table-body {
  margin: 0;
}
</style>
